package com.cpd.blog.config;

import com.cpd.blog.common.handler.LoginInterceptor;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 跨域拦截器配置
 *
 * @author Copydown
 * @date 2021/8/14 16:21
 */
@Configuration
@RequiredArgsConstructor
public class WebMvcConfig implements WebMvcConfigurer {

    private final LoginInterceptor loginInterceptor;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        //跨域配置，不可设置为*，不安全, 前后端分离项目，可能域名不一致
        //本地测试 端口不一致 也算跨域
        registry.addMapping("/**").allowedOrigins(
                "http://localhost:80",
                "http://localhost:7888",
                "http://1.12.244.138",
                "https://1.12.244.138",
                "http://copydown.space",
                "http://www.copydown.space"
                );
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // TODO：拦截器配置写在 yml里
        registry.addInterceptor(loginInterceptor).addPathPatterns(
                "/comments/create/change",
                "/articles/publish",
                "/articles/edit",
                "/articles/mine",
                "/articles/delete",
                "/upload",
                "/user/editNickName",
                "/user/editPassword",
                "/user/sendEmail"
        ).excludePathPatterns(
                "/doc.*"
        );
    }
}
